//查询已通过审核的产品
let dFileList=[];
let arr=[];
function getMApply() {
    $.ajax({
        url:"/inputmanagerController/getFile1",
        type:"get",
        dataType:"json",
        success:function (data) {
            dFileList=data;
            let tbody = $("#tbody-MFile");
            let tr = "";
            for (let i = 0; i < data.length; i++) {
                tr+=" <tr >\n" +
                    "                        <td>"+data[i].productId+"</td>\n" +
                    "                        <td>"+data[i].productName+"</td>\n" +
                    "                        <td>"+data[i].type+"</td>\n" +
                    "                        <td>"+data[i].amoutUnil+"</td>\n" +
                    "                        <td>"+data[i].realCostPrice+"</td>\n" +
                    "                        <td><button onclick='addInTable("+i+")'>生产</button></td>\n" +
                    "                    </tr>";
            }
            //添加到表格
            tbody.html(tr);
        }
    })

    $("input[name='register']").val(parent.document.getElementsByClassName("user_name")[0].innerHTML)
}

//添加产品的方法
function addInTable(index){
    let flag=true;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] == index){
            flag=false;
        }
    }
    if (flag){
        arr.push(index+'');
        let dFileListElement = dFileList[index];
        $("#tbody").append("<tr>\n" +
            "                <td><input value='"+index+"' class=\"checkbox-index\" type=\"checkbox\"></td>\n" +
            "                <td><input value='"+dFileListElement.productName+"' readonly type=\"text\" name=\"productName\"></td>\n" +
            "                <td><input value='"+dFileListElement.productId+"' readonly type=\"text\" name=\"productId\"></td>\n" +
            "                <td><input value='"+dFileListElement.productDescribe+"' readonly type=\"text\" name=\"productDescribe\"></td>\n" +
            "                <td><input type=\"number\" min='1' name=\"amount\" value=\"1\" onchange='changeAmount(this,"+dFileListElement.realCostPrice+")'></td>\n" +
            "                <td>"+dFileListElement.amoutUnil+"</td>\n" +
            "                <td class=\"danjia\">"+dFileListElement.realCostPrice+"</td>\n" +
            "                <td class=\"xiaoji\">"+dFileListElement.realCostPrice+"</td>\n" +
            "            </tr>");

        //置空总金额和总数量
        $("#totalCount").html("");
        $("#totalPrice").html("");
        $("#btn-save").hide();
    }

}


//加载方法
$(function (){
    getMApply()
})


//改变数量
function changeAmount(that,price){
    let amount = $(that).val();
    $(that).parent().next().next().next().html(amount*price);

    //置空总金额和总数量
    $("#totalCount").html("");
    $("#totalPrice").html("");
    $("#btn-save").hide();
}

//点击删除产品
$("#deleteProduct").click(function (){
    let ck = $(".checkbox-index:checked");
    if (ck.length==0){
        alert("请先选择要删除的产品");
    }else{
        ck.each(function (i,e){
            var index = arr.indexOf($(e).val());
            arr.splice(index, 1);
            $(e).parent().parent().remove();
        })
    }
    //置空总金额和总数量
    $("#totalCount").html("");
    $("#totalPrice").html("");
    $("#btn-save").hide();
})

//点击预览
function preview(){
    let ck = $(".checkbox-index");
    if (ck.length!=0){
        //获取数量和小计
        let xiaoji = $(".xiaoji");
        let amounts = $("input[name='amount']");

        //定义总数量和总金额
        let totalCount=0;
        let totalPrice=0;
        amounts.each(function (i,e){
            totalCount+=parseInt($(e).val());
        })
        xiaoji.each(function (i,e){
            totalPrice+=parseFloat($(e).html());
        })

        $("#totalCount").html(totalCount);
        $("#totalPrice").html(totalPrice);
        $("#btn-save").show();
    }else{
        alert("请先选择产品再预览")
    }

}

//提交生产计划单
function saveMApply(){

    //获取四个input框
    let productNames = $("input[name='productName']");
    let productIds = $("input[name='productId']");
    let productDescribes = $("input[name='productDescribe']");
    let amounts = $("input[name='amount']");

    //定义数组存值
    let mApplyArray=[];
    productIds.each(function (i,e){
        let mApply={
            register:$("input[name='register']").val(),
            designer:$("input[name='designer']").val(),
            deptId:0,
            remark:$("textarea[name='remark']").val(),
            productName:productNames[i].value,
            productId:productIds[i].value,
            productDescribe:productDescribes[i].value,
            amount:amounts[i].value,
        }
        //加入数组
        mApplyArray[i]=mApply;
    })
    if (confirm("是否提交该生产计划单？")){
        $.ajax({
            url: "/DHMApply/add",
            type: "post",
            contentType:"application/json;charset=utf-8",
            data:JSON.stringify(mApplyArray),
            success:function (data){
                alert("添加成功");
                parent.document.getElementById('iframeId').contentWindow.location.reload(true)
            }
        })
    }

}